﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client;
using MapMaker.Engine.Core.Models;

namespace MapMaker.Engine.Core
{
    class RavenResource<T> : IResource<T> where T : DataModel
    {
        private IDocumentStore DocumentStore { get; set; }
        public RavenResource(IDocumentStore store)
        {
            DocumentStore = store;
            DocumentStore.Conventions.RegisterIdConvention<Actor>((dbname, commands, user) => "actors/" + user.Name);

        }



        private void WithSessionDoc(Action<IDocumentSession> action)
        {
            using (var session = DocumentStore.OpenSession())
            {
                action(session);
                session.SaveChanges();
            }
        }

        private void WithSessionDoc(Func<IDocumentSession, bool> action)
        {
            using (var session = DocumentStore.OpenSession())
            {
                if(action(session))
                  session.SaveChanges();
            }
        }
    }
}
